package com.addcn.pushlibrary.jpush;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

import com.addcn.pushlibrary.LPush;
import com.addcn.pushlibrary.bean.PMessage;

import org.json.JSONException;
import org.json.JSONObject;

import static android.os.PowerManager.*;

/**
 * author:luwenbo
 * created on date : 2019-05-14
 * content:
 * describe:
 */
public class LJpushReceiver extends JPushMessageReceiver {

    private String TAG = "LJpushReceiver";

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        //super.onMessage(context, customMessage);
        Log.e(TAG, "==onMessage:" + customMessage.toString());
        PMessage pMessage = getMessage(customMessage);
        PowerManager pm = (PowerManager) context.getSystemService(context.POWER_SERVICE);
        PowerManager.WakeLock mWakelock = pm.newWakeLock(ACQUIRE_CAUSES_WAKEUP | SCREEN_DIM_WAKE_LOCK, "JpushTag:");
        mWakelock.acquire(1000);
        mWakelock.release();
        LPush.getInstance(context).onMsgReceiver(pMessage, true);
    }


    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
        super.onMultiActionClicked(context, intent);
        Log.e(TAG, "==onMultiActionClicked");
    }

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        //super.onNotifyMessageArrived(context, notificationMessage);
        Log.e(TAG, "==onNotifyMessageArrived:" + notificationMessage.toString());
        PMessage pMessage = getMessage(notificationMessage);
        PowerManager pm = (PowerManager) context.getSystemService(context.POWER_SERVICE);
        PowerManager.WakeLock mWakelock = pm.newWakeLock(ACQUIRE_CAUSES_WAKEUP | SCREEN_DIM_WAKE_LOCK, "JpushTag:");
        mWakelock.acquire(1000);
        mWakelock.release();
        LPush.getInstance(context).onMsgReceiver(pMessage, false);
    }


    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        //super.onNotifyMessageOpened(context, notificationMessage);
        PMessage pMessage = getMessage(notificationMessage);
        LPush.getInstance(context).onNotificationClicked(pMessage);
        Log.e(TAG, "==onNotifyMessageOpened:" + notificationMessage.toString());
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        super.onCommandResult(context, cmdMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    @Override
    public void onConnected(Context context, boolean b) {
        super.onConnected(context, b);
    }

    @Override
    public void onNotificationSettingsCheck(Context context, boolean b, int i) {
        super.onNotificationSettingsCheck(context, b, i);
    }

    @Override
    public void onRegister(Context context, String s) {
        super.onRegister(context, s);
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageDismiss(context, notificationMessage);
        Log.e(TAG, "==onNotifyMessageDismiss:" + notificationMessage.toString());
    }


    private PMessage getMessage(Intent intent) {
        Bundle bundle = intent.getExtras();
        PMessage pMessage = new PMessage();
        String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
        try {
            if (extra != null) {
                JSONObject extraObj = new JSONObject(extra);
                pMessage.setId(bundle.getString("msg_id") == null ? "" : bundle.getString("msg_id"));
                pMessage.setUrl(extraObj.getString("url"));
                pMessage.setClean(extraObj.getString("clean_notification"));
                pMessage.setExtra(extraObj.getString("extra") == null ? "" : extraObj.getString("extra"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        pMessage.setTitle(bundle.getString(JPushInterface.EXTRA_TITLE));
        pMessage.setContent(bundle.getString(JPushInterface.EXTRA_MESSAGE));
        pMessage.setBigThumb(bundle.getString(JPushInterface.EXTRA_BIG_PIC_PATH));
        pMessage.setFromType(PMessage.FROM_JPUSH);
        return pMessage;
    }

    private PMessage getMessage(NotificationMessage notificationMessage) {
        PMessage pMessage = new PMessage();
        String extra = notificationMessage.notificationExtras;
        try {
            if (extra != null) {
                JSONObject extraObj = new JSONObject(extra);
                pMessage.setId(extraObj.getString("msg_id") == null ? "" : extraObj.getString("msg_id"));
                pMessage.setUrl(extraObj.getString("url"));
                String clean = "0";
                if (extraObj.optString("clean_notification")!=null && !extraObj.optString("clean_notification").equals("")){
                    clean = extraObj.optString("clean_notification");
                }
                pMessage.setClean(clean);
                if(!extraObj.isNull("extra")){
                    pMessage.setExtra(extraObj.getString("extra") == null ? "" : extraObj.getString("extra"));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        pMessage.setTitle(notificationMessage.notificationTitle);
        pMessage.setContent(notificationMessage.notificationContent);
        pMessage.setBigThumb(notificationMessage.notificationBigPicPath);
        pMessage.setFromType(PMessage.FROM_JPUSH);
        return pMessage;
    }

    private PMessage getMessage(CustomMessage customMessage) {
        PMessage pMessage = new PMessage();
        String extra = customMessage.extra;
        try {
            if (extra != null) {
                JSONObject extraObj = new JSONObject(extra);
                pMessage.setId(extraObj.getString("msg_id") == null ? "" : extraObj.getString("msg_id"));
                pMessage.setUrl(extraObj.getString("url"));
                pMessage.setTitle(extraObj.getString("title")==null?"8891汽車":extraObj.getString("title"));
                String clean = "0";
                if (extraObj.optString("clean_notification")!=null && !extraObj.optString("clean_notification").equals("")){
                    clean = extraObj.optString("clean_notification");
                }
                pMessage.setClean(clean);
                if (!extraObj.isNull("image")){
                    pMessage.setBigThumb(extraObj.getString("image"));
                }
                if (!extraObj.isNull("extra")){
                    pMessage.setExtra(extraObj.optString("extra") == null ? "" : extraObj.getString("extra"));
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //pMessage.setTitle(customMessage.title);
        pMessage.setContent(customMessage.message);
        pMessage.setFromType(PMessage.FROM_JPUSH);
        return pMessage;
    }
}
